"""
演示JSON数据和python字典的相互转换

"""

# 准备列表，列表内每一个元素都是字典，将其转换成JSON
import json
data = [{"name":"tom","age":11},{"name":"jack","age":13},{"name":"xixi","age":16}]


json_str = json.dumps(data,ensure_ascii=False)
print(type(json_str))
print(json_str)

# 准备字典 将字典转换成JSON
d = {"name":"zhoujielun","address":"taibei"}
json_str = json.dumps(d,ensure_ascii=False)
print(type(json_str))
print(json_str)


# 将JSON 字符串转换为python数据类型[{k:v,m:n},{k:v,m:n}]

s = '[{"name": "tom", "age": 11}, {"name": "jack", "age": 13}, {"name": "xixi", "age": 16}]'
l = json.loads(s)                       
print(type(l))
print(l)


# 将JSON字符串转换为python数据类型{k:v,k:v}
s = '{"name": "zhoujielun", "address": "taibei"}'
d = json.loads(s)
print(type(d))
print(d)
